package com.yixing.tech.wms.service.impl;

import com.yixing.tech.common.base.BusinessException;
import com.yixing.tech.common.service.AbstractWmsService;
import com.yixing.tech.wms.service.StorageAreaService;
import org.bson.Document;
import org.bson.types.ObjectId;
import org.springframework.stereotype.Service;

import static com.yixing.tech.common.CommonConstant.*;

/**
 * StorageLocationService
 *
 * @author yixing tech
 * @since 1.0.0
 */
@Service
public class StorageAreaServiceImpl extends AbstractWmsService implements StorageAreaService {

    @Override
    public Document validateGetFirstArea(AreaType areaType) throws BusinessException {
        if(areaType == null) {
            throw new BusinessException("请指定类型");
        }
        Document area = simpleFindOne("entity_" + STOCK_AREA, "storageType", newDocument("id", new ObjectId(areaType.getCode())));
        if(area == null) {
            throw new BusinessException("该类型的库区不存在");
        }
        return area;
    }

    @Override
    public String validateGetFirstAreaCode(AreaType areaType) throws BusinessException {
        return validateGetFirstArea(areaType).getString("code");
    }
}
